const fs = require("fs");
const puppeteer = require("puppeteer");
let curIndex = 506;
puppeteer
  .launch({
    executablePath: "d:\\code\\node-crawler\\chrome\\chrome.exe",
    headless: false,
    userDataDir: "./userdata", //缓存网站数据，用于记录登陆状态
    defaultViewport: null,
  })
  .then(async (browser) => {
    const page = await browser.newPage();
    //https://www.bqxs520.com/load.html?v1#927691-59978424-269079420-0
    //https://m.daaixs.com/INW/1.html 这个网址的也不错
    //https://45a4872a5.bi24.cc/book/15682/1.html
    // https://www.qiushubang.me/read/11015166/188453631.html
    await page.goto(
      "https://www.bqxs520.com/load.html?v1#927691-59978424-269082665-0",
      {
        waitUntil: "domcontentloaded",
      }
    );

    await parsePage(page);
  });

async function sleep(mesec = 10000) {
  return new Promise((resolve) => setTimeout(resolve, mesec));
}

let lastTitle;
async function parsePage(page) {
  await sleep(2000);
  const fileName = "./bqxs520-opponent-" + Math.floor(curIndex / 1500) + ".txt";
  try {
    let title = await page.$eval("#ctitle", (el) => el.innerText);
    if (title !== lastTitle) {
      fs.appendFileSync(fileName, "\n\n#" + title + "#\n\n");
      lastTitle = title;
    }

    let text = await page.$eval("#content", (el) => el.innerText);
    text = text
      .replace(
        "一秒记住【完本神站】手机用户输入地址：m.wanbentxt.com\n\n.，",
        ""
      )
      .replace("一秒记住【完本神站】手机用户输入地址：m.wanbentxt.com\n", "")
      .replace(
        "支持（完本神站）把本站分享那些需要的小伙伴！找不到书请留言！",
        ""
      );

    fs.appendFileSync(fileName, text);
    //输出当前状态
    console.log(`已完成：第${curIndex++}页, 当前URL: ` + page.url());
    //递归调用本函数以获取下一章内容
    await getNextPage(page);
  } catch (error) {
    console.error("🚀 ~ parsePage ~ error:", error);
    parsePage(page);
  }
}

async function getNextPage(page) {
  await page.$eval("#next", (e) => e.click());
  await parsePage(page);
}
